﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace HydrographicSurvey.HYTCPServer {
    //class Program
    //{
    //    static void Main(string[] args)
    //    {
    //        TcpListener server = null;
    //        try {
    //            // Set the TcpListener on port 13000.
    //            Int32 port = 13000;
    //            IPAddress localAddr = IPAddress.Any;

    //            // TcpListener server = new TcpListener(port);
    //            server = new TcpListener(localAddr, port);
    //            // Start listening for client requests.
    //            server.Start();
    //            ClientAsync clientAsync = new ClientAsync(null);
    //            while (true) {
    //                clientAsync.DoBeginAcceptTcpClient(server);
    //            }

    //        } catch (SocketException e) {
    //            Console.WriteLine("SocketException: {0}", e);
    //        } finally {
    //            // Stop listening for new clients.
    //            server.Stop();
    //        }


    //        Console.WriteLine("\nHit enter to continue...");
    //        Console.Read();
    //    }
    //}
    class Program {
        static void Main(string[] args) {
            IPAddress local = IPAddress.Any;
            IPEndPoint iep = new IPEndPoint(local, 13000);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // 将套接字与本地终结点绑定   
            server.Bind(iep);

            //在本地13000端口号上进行监听    
            server.Listen(100);
            Console.WriteLine("等待客户机进行连接......");
            while (true) {
                // 得到包含客户端信息的套接字  
                Socket client = server.Accept();

                //创建消息服务线程对象  
                ClientThread newclient = new ClientThread(client);

                //把ClientThread 类的ClientService方法委托给线程  
                Thread newthread = new Thread(new ThreadStart(newclient.ClientService));

                // 启动消息服务线程  
                newthread.Start();
            }
        }
    }
}
